﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace shfa.snomtastic.systray.components
{
    public class WindowManager
    {
        public delegate bool WindowEnumDelegate(IntPtr hwnd, int lParam);


        private List<IntPtr> WindowHandles;

        public WindowManager() { }

        public void HideWindow()
        {
            bool hidden = false;
            while (!hidden)
            {
                WindowHandles = new List<IntPtr>();
                EnumCommunicatorWindows();
                if (HideFoundWindows())
                    hidden = true;

            }
        }

        private void EnumCommunicatorWindows()
        {
            WindowEnumDelegate del = new WindowEnumDelegate(HideCommunicatorWindowDelegate);
            Win32.EnumWindows(del, 0);
        }

        private bool HideCommunicatorWindowDelegate(IntPtr hwnd, int lParam)
        {

            WindowInfo wi = new WindowInfo(hwnd);
            if (wi.GetWindowClass == "MsCommunicatorToastPopup")
                if (wi.GetWindowText.IndexOf("Phone Call") == 0)
                        WindowHandles.Add(hwnd);

            return true;
        }

        private bool HideFoundWindows()
        {

            bool bHid = false;

            foreach (IntPtr i in WindowHandles)
            {
                WindowInfo wi = new WindowInfo(i);
                if (wi.GetWindowClass == "MsCommunicatorToastPopup")
                    if (wi.GetWindowText.IndexOf("Phone Call") == 0)
                        if (Win32.IsWindowVisible(i))
                            if (Win32.ShowWindowAsync(i, Win32.SW_HIDE))
                            {
                                bHid = true;
                            }
            }

            return bHid;
        }






    }
}
